/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lestr.udm.core2;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pibonnin
 */
public abstract class GenericDataManager<S extends IDataManager.ISession, P extends IDataManager.IParameters> implements IDataManager<P> {

    
    @Override
    public ISession openSession() throws GdmException {
        
        Type type = getClass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) type;
        Class<S> classe = (Class<S>) paramType.getActualTypeArguments()[0];
        
        try {
            return classe.newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(GenericDataManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new GdmException(ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GenericDataManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new GdmException(ex);
        }
        
    }

    
    @Override
    public abstract P getParameters();
    

    public static class AbstractSession implements IDataManager.ISession {


        @Override
        public boolean addData(String id) throws GdmException {
            return false;
        }


        @Override
        public boolean existsData(String id) throws GdmException {
            return false;
        }


        @Override
        public boolean removeData(String id) throws GdmException {
            return false;
        }


        @Override
        public boolean setDataValue(String id,
                                    InputStream value) throws GdmException {
            return false;
        }


        @Override
        public InputStream getDataValue(String id) throws GdmException {
            return null;
        }


        @Override
        public long getDataLength(String id) throws GdmException {
            return 0;
        }


        @Override
        public boolean commit() throws GdmException {
            return true;
        }


        @Override
        public boolean close() throws GdmException {
            return true;
        }

        
    }
    
    
    public static class AbstractParameters implements IDataManager.IParameters 
    {
    }
    
    
}// END Class AbstractDataManager
